Java用freemarker导出word实用示例 |
您所在的位置:网站首页 › freemarker word模板 › Java用freemarker导出word实用示例 |
最近一个项目要导出word文档,折腾老半天,发现还是用freemarker的模板来搞比较方便省事,现总结一下关键步骤,供大家参考,这里是一个简单的试卷生成例子。 一、模板的制作 先用Word做一个模板,如下图: 二、编程实现 这里用到了freemarker-2.3.13.jar包,代码如下: package common; import java.io.BuffereDWriter; import java.io.file; import java.io.fileNotFoundException; import java.io.fileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.UnsupportedEnCodingException; import java.io.Writer; import java.util.Map; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; public class documentHandler { private Configuration configuration = null; public documentHandler() { configuration = new Configuration(); configuration.setdefaultencoding("utf-8"); } public voID createDoc(Map dataMap,String filename) throws UnsupportedEnCodingException { //dataMap 要填入模本的数据文件 //设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库装载, //这里我们的模板是放在template包下面 configuration.setClassForTemplateLoading(this.getClass(),"/template"); Template t=null; try { //test.ftl为要装载的模板 t = configuration.getTemplate("fctestpaper.ftl"); } catch (IOException e) { e.printstacktrace(); } //输出文档路径及名称 file outfile = new file(filename); Writer out = null; fileOutputStream fos=null; try { fos = new fileOutputStream(outfile); OutputStreamWriter oWriter = new OutputStreamWriter(fos,"UTF-8"); //这个地方对流的编码不可或缺,使用main()单独调用时,应该可以,但是如果是web请求导出时导出后word文档就会打不开,并且包XML文件错误。主要是编码格式不正确,无法解析。 //out = new BuffereDWriter(new OutputStreamWriter(new fileOutputStream(outfile))); out = new BuffereDWriter(oWriter); } catch (fileNotFoundException e1) { e1.printstacktrace(); } try { t.process(dataMap,out); out.close(); fos.close(); } catch (TemplateException e) { e.printstacktrace(); } catch (IOException e) { e.printstacktrace(); } //System.out.println("---------------------------"); } }然后是准备数据调用就行,代码如下: package com.havenliu.document; import java.io.UnsupportedEnCodingException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class Main { /** * @param args * @throws UnsupportedEnCodingException */ public static voID main(String[] args) throws UnsupportedEnCodingException {; Map dataMap = new HashMap(); dataMap.put("xyTitle","试卷"); int index = 1; // 选择题 List List1 = new ArrayList();//题目 List List11 = new ArrayList();//答案 index = 1; for (int i = 0; i < 5; i++) { Map map = new HashMap(); map.put("xzn",index + "."); map.put("xztest","( )操作系统允许在一台主机上同时连接多台终端,多个用户可以通过各自的终端同时交互地使用计算机。"); map.put("ans1","A" + index); map.put("ans2","B" + index); map.put("ans3","C" + index); map.put("ans4","D" + index); List1.add(map); Map map1 = new HashMap(); map1.put("fuck",index + "."); map1.put("abc","A" + index); List11.add(map1); index++; } dataMap.put("table1",List1); dataMap.put("table11",List11); // 填空题 List List2 = new ArrayList(); List List12 = new ArrayList(); index = 1; for (int i = 0; i < 5; i++) { Map(); map.put("tkn",index + "."); map.put("tktest","操作系统是计算机系统中的一个___系统软件_______,它管理和控制计算机系统中的___资源_________."); List2.add(map); Map(); map1.put("fill",index + "."); map1.put("def","中级调度" + index); List12.add(map1); index++; } dataMap.put("table2",List2); dataMap.put("table12",List12); // 判断题 List List3 = new ArrayList List13 = new ArrayList |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |